package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.ServiceNodeListEntity;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddServiceNodeViewModel extends AndroidViewModel {
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<String> toast = new MutableLiveData<>();
    public  ServiceNodeListEntity data = null;
    public String id = "";

    public AddServiceNodeViewModel(@NonNull Application application) {
        super(application);
    }




    public void submitdata(Map<String,Object> value){


        ShowLoading.postValue(1);
        Call<BaseEntity> call = !TextUtils.isEmpty(id)?NewAPIManager.getInstance().getEnterPrisenewAPI().saveServiceNode(value):NewAPIManager.getInstance().getEnterPrisenewAPI().uodateServiceNode(value);
        call.enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ShowLoading.postValue(3);
                } else {
                    toast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                ShowLoading.postValue(2);
                toast.postValue(t.getMessage());
            }
        });
    }
}
